package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwbgT;
import cn.e4j.modular.crm.entity.GwbglT;
import cn.e4j.modular.crm.entity.GwgaT;
import cn.e4j.modular.crm.entity.GwgbT;
import cn.e4j.modular.crm.mapper.Gwgi001Mapper;
import cn.e4j.modular.crm.service.Gwgi001Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class Gwgi001ServiceImpl extends ServiceImpl<Gwgi001Mapper, GwgaT> implements Gwgi001Service {
    @Override
    public List<Map<String, Object>> getHeadList(Page<Object> page, Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwgi001Mapper,getHeadList", page, inDto);
//        return this.baseMapper.getHeadList(page,inDto);
    }

    @Override
    public List<Map<String, Object>> getGwgbList(Page<Object> page, Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwgi001Mapper,getGwgbList", page, inDto);
    }

    @Override
    public List<Map<String, Object>> getGwgcList(Page<Object> page, Dto inDto) {
        List<Map<String, Object>> gwgcList = InvokeUtil.invokeBeanMethod("gwgi001Mapper,getGwgcList", page, inDto);

        if (gwgcList != null) {
            gwgcList.forEach(gwgcT -> {
                if(gwgcT.get("GWGD007")==null||gwgcT.get("GWGD010")==null) {
                    gwgcT.put("TOTAL","");
                }else {
                    BigDecimal number = (BigDecimal) gwgcT.get("GWGD007");
                    BigDecimal price = (BigDecimal) gwgcT.get("GWGD010");
                    gwgcT.put("TOTAL",price.multiply(number));
                }
            });
        }
        return gwgcList;
    }

    @Override
    public List<GwgbT> getDetailList(Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwgi001Mapper,getDetailList", inDto);
    }

    @Override
    public List<GwgaT> getGwgaDetailList(Dto inDto) {
        return this.baseMapper.getGwgaDetailList(inDto);
    }

    @Override
    public boolean existedTheParams(GwgaT gwgaT) {
        QueryWrapper<GwgaT> gwgaTQueryWrapper = new QueryWrapper<>();
        gwgaTQueryWrapper.eq("GWGA001", gwgaT.getGWGA001());
        return this.baseMapper.selectList(gwgaTQueryWrapper).size() > 0;
    }

    @Override
    @Transactional
    public void add(List<GwgaT> gwgaTList, List<GwgbT> gwgbTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String ENT = shiroUser.getDBC();
        GwgaT gwgaT = gwgaTList.get(0);

        // 添加招标信息
        gwgaT.setGWGAENT(shiroUser.getDBC());
        gwgaTList.forEach(gwgaT1 -> {
            gwgaT1.setGWGAENT(ENT);
            gwgaT1.insert();
        });

        // 添加中标信息
        gwgbTList.forEach(gwgbT -> {
            gwgbT.setGWGBENT(ENT);
            gwgbT.setGWGB001(gwgaT.getGWGA001());
            gwgbT.insert();
        });

        // 添加商机主题
        GwbgT gwbgT = new GwbgT();
        // 获取归属业务员(客户的创建人)
        String GWAKORIU = this.baseMapper.getGWAKORIU(gwgaT.getGWGA008(),ENT);
        gwbgT.setGWBGENT(ENT);
        gwbgT.setGWBG001(DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
        gwbgT.setGWBG002(gwgaT.getGWGA008());
        gwbgT.setGWBG003("N");
        gwbgT.setGWBG004("N");
        gwbgT.setGWBG005(GWAKORIU);
        gwbgT.setGWBG006(gwgaT.getGWGA001());
        gwbgT.setGWBG007(gwgaT.getGWGA016());
        gwbgT.setGWBGACTI("Y");
        gwbgT.insert();

        // 添加对应的商机主题多语言表
        GwbglT gwbglT = new GwbglT();
        gwbglT.setGWBGLENT(ENT);
        gwbglT.setGWBGL001(gwbgT.getGWBG001());
        gwbglT.setGWBGL002(LangUtils.getLang(shiroUser.getLANG()));
        gwbglT.setGWBGL003(gwgaT.getGWGA002());
        gwbglT.setGWBGL004(gwbgT.getGWBG001());
        gwbglT.insert();
    }

    @Override
    @Transactional
    public void edit(List<GwgaT> gwgaTList, List<GwgbT> gwgbTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwgaT gwgaT = gwgaTList.get(0);

        // 更新招标信息
        UpdateWrapper<GwgaT> gwgaTUpdateWrapper = new UpdateWrapper<>();
        gwgaTUpdateWrapper.eq("GWGA001", gwgaT.getGWGA001());
        gwgaTUpdateWrapper.eq("GWGAENT", shiroUser.getDBC());
        new GwgaT().delete(gwgaTUpdateWrapper);
        gwgaTList.forEach(gwgaT1 -> {
            gwgaT1.setGWGAENT(shiroUser.getDBC());
            gwgaT1.insert();
        });

        // 更新中标信息
        UpdateWrapper<GwgbT> gwgbTUpdateWrapper = new UpdateWrapper<>();
        gwgbTUpdateWrapper.eq("GWGB001", gwgaT.getGWGA001());
        gwgbTUpdateWrapper.eq("GWGBENT", shiroUser.getDBC());
        new GwgbT().delete(gwgbTUpdateWrapper);
        gwgbTList.forEach(gwgbT -> {
            gwgbT.setGWGBENT(shiroUser.getDBC());
            gwgbT.setGWGB001(gwgaT.getGWGA001());
            gwgbT.insert();
        });
    }

    @Override
    @Transactional
    public void delete(GwgaT gwgaT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String ENT = shiroUser.getDBC();
        // 删除招标信息
        UpdateWrapper<GwgaT> gwgaTUpdateWrapper = new UpdateWrapper<>();
        gwgaTUpdateWrapper.eq("GWGA001", gwgaT.getGWGA001());
        gwgaTUpdateWrapper.eq("GWGAENT", ENT);
        gwgaT.delete(gwgaTUpdateWrapper);

        // 删除中标信息
        UpdateWrapper<GwgbT> gwgbTUpdateWrapper = new UpdateWrapper<>();
        gwgbTUpdateWrapper.eq("GWGB001", gwgaT.getGWGA001());
        gwgbTUpdateWrapper.eq("GWGBENT", ENT);
        new GwgbT().delete(gwgbTUpdateWrapper);

//         获取招标信息对应的商机主题编号
        QueryWrapper<GwbgT> gwbgTQueryWrapper = new QueryWrapper<>();
        gwbgTQueryWrapper.eq("GWBG006", gwgaT.getGWGA001());
        GwbgT gwbgT = new GwbgT().selectOne(gwbgTQueryWrapper);
        String GWBG001 = gwbgT.getGWBG001();
        if(GWBG001 != null) {
//         同时删除招标信息对应的商机资料
            UpdateWrapper<GwbgT> gwbgTUpdateWrapper = new UpdateWrapper<>();
            gwbgTUpdateWrapper.eq("GWBG001", GWBG001);
            gwbgTUpdateWrapper.eq("GWBGENT", shiroUser.getDBC());
            new GwbgT().delete(gwbgTUpdateWrapper);

            // 删除对应的商机资料多语言表
            UpdateWrapper<GwbglT> gwbglTUpdateWrapper = new UpdateWrapper<>();
            gwbglTUpdateWrapper.eq("GWBGL001", GWBG001);
            gwbglTUpdateWrapper.eq("GWBGLENT", shiroUser.getDBC());
            new GwbglT().delete(gwbglTUpdateWrapper);
        }
    }

    @Override
    @Transactional
    public void deleteBatch(List<GwgaT> gwgaTList) {
        gwgaTList.forEach(this::delete);
    }

    @Override
    @Transactional
    public void changeStatusBatch(List<GwgaT> gwgaTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwgaTList.forEach(gwgaT -> {
            GwgaT updateGwgaT = new GwgaT();
            updateGwgaT.setGWGAACTI(gwgaT.getGWGAACTI());

            UpdateWrapper<GwgaT> gwgaTUpdateWrapper = new UpdateWrapper<>();
            gwgaTUpdateWrapper.eq("GWGA001", gwgaT.getGWGA001());
            gwgaTUpdateWrapper.eq("GWGAENT", shiroUser.getDBC());

            updateGwgaT.update(gwgaTUpdateWrapper);
        });
    }

    @Override
    public boolean existedGwbh(GwgaT gwgaT) {
        return this.baseMapper.existedGwbh(gwgaT) > 0;
    }

    @Override
    public boolean existedGwgc(GwgaT gwgaT) {
        return this.baseMapper.existedGwgc(gwgaT) > 0;
    }

    @Override
    public List<Map<String, Object>> getGwgaData(Page<Object> page, Dto inDto) {
        return InvokeUtil.invokeBeanMethod("gwgi001Mapper,getGwgaData", page, inDto);
    }
}
